package repo

import (
	"context"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/util"
)

type ProductCustomValueDBData struct {
	repository.BaseDBData
	SomsProductId	int64	`orm:"soms_product_id"`	//商品id
	FieldId	int64	`orm:"field_id"`	//商品自定义字段表ID
	FieldName	string	`orm:"field_name"`	//字段名称
	FieldType	string	`orm:"field_type"`	//字段类型:0:字符串 1:数字
	PrintFlag	string	`orm:"print_flag"`	//是否打印:0:不打印 1:打印
	Sort	int64	`orm:"sort"`	//排序
	Value	string	`orm:"value"`	//该字段填写的值
	
}

type ProductCustomValueDBDataParam struct {
	ProductCustomValueDBData
	Ids       []int64
}

func (*ProductCustomValueDBData) TableName() string {
	return "product_custom_value"
}

func (param *ProductCustomValueDBData) getConditions() []*repository.Condition {
	return []*repository.Condition{
		repository.NewAndCondition(param),
	}
}

func (param *ProductCustomValueDBDataParam) listConditions() []*repository.Condition {
	var conditions []*repository.Condition

	if param.Ids != nil && len(param.Ids) > 0 {
    		conditions = append(conditions, repository.NewAndCondition("id in ?", param.Ids))
    }

	if param.SomsProductId != 0 {		conditions = append(conditions, repository.NewAndCondition("soms_product_id = ?", param.SomsProductId))	}if param.SomsProductId != 0 {		conditions = append(conditions, repository.NewAndCondition("soms_product_id = ?", param.SomsProductId))	}
	if param.FieldId != 0 {		conditions = append(conditions, repository.NewAndCondition("field_id = ?", param.FieldId))	}if param.FieldId != 0 {		conditions = append(conditions, repository.NewAndCondition("field_id = ?", param.FieldId))	}
	if param.FieldName != "" {
		conditions = append(conditions, repository.NewAndCondition("field_name = ?", param.FieldName))
	}

	if param.FieldType != "" {
		conditions = append(conditions, repository.NewAndCondition("field_type = ?", param.FieldType))
	}

	if param.PrintFlag != "" {
		conditions = append(conditions, repository.NewAndCondition("print_flag = ?", param.PrintFlag))
	}

	if param.Sort != 0 {		conditions = append(conditions, repository.NewAndCondition("sort = ?", param.Sort))	}
	if param.Value != "" {
		conditions = append(conditions, repository.NewAndCondition("value = ?", param.Value))
	}

	
	
	return conditions
}

type ProductCustomValueRepo interface {
	repository.BaseCommonRepo
	Get(context.Context, *ProductCustomValueDBData) (*ProductCustomValueDBData, error)
	Count(context.Context, *ProductCustomValueDBDataParam) (int64, error)
	List(context.Context, string, int32, int32, *ProductCustomValueDBDataParam) (*[]ProductCustomValueDBData, int64, error)
	SimpleList(ctx context.Context, order string, pageNum, pageSize int32, param *ProductCustomValueDBDataParam) (*[]ProductCustomValueDBData, int64, error)
}

type productCustomValueRepo struct {
	repository.BaseRepo
}

func (r *productCustomValueRepo) Get(ctx context.Context, param *ProductCustomValueDBData) (*ProductCustomValueDBData, error) {
	data, err := r.BaseGet(ctx, repository.NewQueryBuilder().Where(param.getConditions()))
	if data == nil {
		return nil, handler.HandleError(err)
	}
	return data.(*ProductCustomValueDBData), handler.HandleError(err)
}

func (r *productCustomValueRepo) List(ctx context.Context, order string, pageNum, pageSize int32, param *ProductCustomValueDBDataParam) (*[]ProductCustomValueDBData, int64, error) {
	data, count, err := r.BaseList(ctx, repository.NewListQueryBuilder(order, pageNum, pageSize).Where(param.listConditions()))
	if data == nil {
		return nil, count, handler.HandleError(err)
	}
	return data.(*[]ProductCustomValueDBData), count, handler.HandleError(err)
}


func (r *productCustomValueRepo) SimpleList(ctx context.Context, order string, pageNum, pageSize int32, param *ProductCustomValueDBDataParam) (*[]ProductCustomValueDBData, int64, error) {
	conditions := []string{}
	params := []interface{}{}


	if param.SomsProductId != "" {
		             conditions = append(conditions, "soms_product_id = ?")
		             params = append(params, param.SomsProductId)
         }
	if param.FieldId != "" {
		             conditions = append(conditions, "field_id = ?")
		             params = append(params, param.FieldId)
         }
	if param.FieldName != "" {
		             conditions = append(conditions, "field_name = ?")
		             params = append(params, param.FieldName)
         }
	if param.FieldType != "" {
		             conditions = append(conditions, "field_type = ?")
		             params = append(params, param.FieldType)
         }
	if param.PrintFlag != "" {
		             conditions = append(conditions, "print_flag = ?")
		             params = append(params, param.PrintFlag)
         }
	if param.Sort != "" {
		             conditions = append(conditions, "sort = ?")
		             params = append(params, param.Sort)
         }
	if param.Value != "" {
		             conditions = append(conditions, "value = ?")
		             params = append(params, param.Value)
         }
	

	var count int64
	err := r.Db.WithContext(ctx).Model(r.Model).Where(strings.Join(conditions, " and "), params...).Count(&count).Error
	if err != nil {
		return nil, 0, handler.HandleError(err)
	}
	if count == 0 {
		return nil, 0, nil
	}
	var result []ProductCustomValueDBData
	db := r.Db.WithContext(ctx).Where(strings.Join(conditions, " and "), params...)
	if order != "" {
		db = db.Order(order)
	}
	if pageNum > 0 && pageSize > 0 {
		db = db.Offset(int((pageNum - 1) * pageSize))
		db = db.Limit(int(pageSize))
	}
	err = db.Find(&result).Error
	return &result, count, handler.HandleError(err)
}


func (r *productCustomValueRepo) Count(ctx context.Context, param *ProductCustomValueDBDataParam) (int64, error) {
	count, err := r.BaseCount(ctx, repository.NewQueryBuilder().Where(param.listConditions()))
	return count, handler.HandleError(err)
}
